package com.codegeek.project.slope.controller;

import com.codegeek.common.utils.poi.ExcelUtil;
import com.codegeek.framework.aspectj.lang.annotation.Log;
import com.codegeek.framework.aspectj.lang.enums.BusinessType;
import com.codegeek.framework.web.controller.BaseController;
import com.codegeek.framework.web.domain.AjaxResult;
import com.codegeek.framework.web.page.TableDataInfo;
import com.codegeek.project.slope.domain.*;
import com.codegeek.project.slope.service.ISlopeAlarmService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;

/**
 * 告警记录Controller
 *
 * @author codegeek
 * @date 2020-02-17
 */
@RestController
@RequestMapping("/slope/alarm")
@Api(value = "告警记录Controller", tags = {"告警记录操作接口"})
public class SlopeAlarmController extends BaseController {
    @Autowired
    private ISlopeAlarmService slopeAlarmService;

    /**
     * 查询告警记录列表
     */
    @ApiOperation("查询告警记录列表")
    @PreAuthorize("@ss.hasPermi('slope:alarm:list')")
    @GetMapping("/list")
    public TableDataInfo list(SlopeAlarm slopeAlarm) {
        startPage();
        if (slopeAlarm.getEndTime() != null) {
            slopeAlarm.setEndTime(slopeAlarm.getEndTime() + " 23:59:59");
        }
        List<SlopeAlarm> list = slopeAlarmService.selectSlopeAlarmList(slopeAlarm);
        return getDataTable(list);
    }

    /**
     * 查询首页告警记录列表
     */
    @ApiOperation("查询告警记录列表")
    @PreAuthorize("@ss.hasPermi('slope:alarm:index')")
    @GetMapping("/indexList")
    public TableDataInfo indexList(SlopeAlarm slopeAlarm) {
        startPage();
        if (slopeAlarm.getEndTime() != null) {
            slopeAlarm.setEndTime(slopeAlarm.getEndTime() + " 23:59:59");
        }
        slopeAlarm.setAlarmState(0);
        List<SlopeAlarm> list = slopeAlarmService.selectSlopeAlarmList(slopeAlarm);
        return getDataTable(list);
    }

    /**
     * 导出告警记录列表
     */
    @ApiOperation("导出告警记录列表")
    @Log(title = "告警管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('slope:alarm:export')")
    @GetMapping("/export")
    public AjaxResult export(SlopeAlarm slopeAlarm) {
        List<SlopeAlarm> list = slopeAlarmService.selectSlopeAlarmList(slopeAlarm);
        List<SlopeAlarmReport> alarmReports = new LinkedList<>();
        for (SlopeAlarm alarm : list) {
            SlopeSensor sensor = alarm.getAlarmSensor();
            SlopeSensorType sensorType = sensor.getSensorType();
            SlopeDevice device = sensor.getDevice();
            SlopePoint point = device.getPoint();
            SlopeMonitorItem item = alarm.getMonitorItem();
            //todo 具体项目
            SlopeAlarmReport report = SlopeAlarmReport.builder()
                    .id(alarm.getAlarmId())
                    .detail(sensor.getMonitorDetail())
                    .pointName(point.getPointName())
                    .deviceType(sensorType.getTypeName())
                    .deviceName(device.getDeviceName())
                    .itemName(item.getItemName())
                    .coordinate(point.getLon() + "," + point.getLat())
                    .workingStatus(device.getState())
                    .alarmLevel(alarm.getAlarmLevel())
                    .threshold(item.getThreshold1())
                    .alarmThreadhold(alarm.getAlarmThreadhold())
                    .state(alarm.getAlarmState())
                    .alarmValue(alarm.getAlarmValue())
                    .alarmTime(alarm.getAlarmTime())
                    .build();
            alarmReports.add(report);
        }
        ExcelUtil<SlopeAlarmReport> util = new ExcelUtil<>(SlopeAlarmReport.class);
        return util.exportExcel(alarmReports, "alarm");
    }

    /**
     * 获取告警记录详细信息
     */
    @ApiOperation("获取告警记录详细信息")
    @GetMapping(value = "/getInfo/{alarmId}")
    public AjaxResult getInfo(@PathVariable("alarmId") Long alarmId) {
        return AjaxResult.success(slopeAlarmService.selectSlopeAlarmById(alarmId));
    }

    /**
     * 新增告警记录
     */
    @ApiOperation("新增告警记录")
    @Log(title = "告警管理", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('slope:alarm:add')")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SlopeAlarm slopeAlarm) {
        return toAjax(slopeAlarmService.insertSlopeAlarm(slopeAlarm));
    }

//    /**
//     * 修改告警记录
//     */
//    @ApiOperation("修改告警记录")
//    @Log(title = "告警管理", businessType = BusinessType.UPDATE)
//    public AjaxResult edit(@RequestBody SlopeAlarm slopeAlarm)
//    {
//        return toAjax(slopeAlarmService.updateSlopeAlarm(slopeAlarm));
//    }
//

    /**
     * 删除告警记录
     */
    @ApiOperation("删除告警记录")
    @Log(title = "告警管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove/{alarmIds}")
    @PreAuthorize("@ss.hasPermi('slope:alarm:remove')")
    public AjaxResult remove(@PathVariable Long[] alarmIds) {
        return toAjax(slopeAlarmService.deleteSlopeAlarmByIds(alarmIds));
    }

    /**
     * 首页预警分类统计
     */
    @ApiOperation("首页预警分类统计")
    @GetMapping("/indexAlarmSum")
    public AjaxResult indexAlarmSum() {
        return AjaxResult.success(slopeAlarmService.countSlopeAlarmLevel());
    }

    /**
     * 告警处置
     */
    @ApiOperation("告警处置")
    @Log(title = "告警管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('slope:alarm:change')")
    @PostMapping("/changAlarmStatus")
    public AjaxResult changAlarmStatus(Long alarmId, Integer state) {
        return AjaxResult.success(slopeAlarmService.changeAlarmStatusById(alarmId, state));
    }

    /**
     * 告警批量处置，批量关闭
     */
    @ApiOperation("告警批量处置,批量关闭")
    @Log(title = "告警管理", businessType = BusinessType.UPDATE)
    @PostMapping("/changSelectedAlarmStatus")
    @PreAuthorize("@ss.hasPermi('slope:alarm:listChange')")
    public AjaxResult changSelectedAlarmStatus(String idArr, Integer state) {
        return AjaxResult.success(slopeAlarmService.changSelectedAlarmStatus(idArr, state));
    }

    /**
     * 根据告警等级显示告警列表
     */
    @ApiOperation("根据告警等级显示告警列表")
    @GetMapping("selectAlarmListByLevel")
    public AjaxResult selectAlarmListByLevel(@RequestParam("alarmLevel") Integer alarmLevel) {
        return AjaxResult.success(slopeAlarmService.selectAlarmListByLevel(alarmLevel));
    }

    /**
     * 一键关闭
     */
    @ApiOperation("告警管理一键关闭,")
    @Log(title = "告警管理一键关闭", businessType = BusinessType.UPDATE)
    @PostMapping("/oneClickClose")
    @PreAuthorize("@ss.hasPermi('slope:alarm:close')")
    public AjaxResult oneClickClose() {
        return AjaxResult.success(slopeAlarmService.oneClickClose());
    }
}
